package com.lm.services.hibernate.plane;

import java.util.ArrayList;
import java.util.List;

import com.lm.entity.bean.PlanItinerary;
import com.lm.entity.bean.PlaneChildrenOrder;
import com.lm.entity.bean.PlaneGaiqianInfo;
import com.lm.entity.bean.PlaneOrderPassenger;
import com.lm.entity.bean.PlaneParentOrder;
import com.lm.entity.vo.plane.PlaneOrderVO;
import com.lm.services.iface.plane.IGaiqianOrderService;
import com.lm.utils.Tools;
import com.lm.utils.page.PageBean;
import com.lm.utils.page.Paginate;

public class GaiqianOrderService extends Paginate implements
		IGaiqianOrderService {

	@Override
	public PlaneGaiqianInfo getObject(PlaneGaiqianInfo gaiqianInfo) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List getList(PlaneGaiqianInfo gaiqianInfo, PageBean pageBean) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List getListByDate(PlaneGaiqianInfo gaiqianInfo, PageBean pageBean) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		if (!Tools.isNotNullOrEmpty(gaiqianInfo.getStartTime())) {
			gaiqianInfo.setStartTime(Tools.returnDefaultDate(0));
		}
		if (Tools.isNotNullOrEmpty(gaiqianInfo.getStartTime())) {
			hql.append("AND addDate >='").append(gaiqianInfo.getStartTime())
					.append("'");
		}
		if (Tools.isNotNullOrEmpty(gaiqianInfo.getEndTime())) {
			hql.append("and addDate<='").append(gaiqianInfo.getEndTime())
					.append("'");
		}
		if (Tools.isNotNullOrEmpty(gaiqianInfo.getOperator())) {
			hql.append("and operator='").append(gaiqianInfo.getOperator())
					.append("'");
		}
		List list = null;
		try {
			if(pageBean.isShowPageToolsMenu()==true){
				pageBean.setTotalCountSQL("SELECT COUNT(*) FROM PlaneGaiqianInfo WHERE 1=1 "
						+ hql.toString());
				pageBean.setCount(getTotalCount(pageBean).intValue());
				pageBean.setPageToolsMenu(getToolsMenu(pageBean));
			}
			if(Tools.isNotNullOrEmpty(hql)){
				pageBean.setListSQL("from PlaneGaiqianInfo where 1=1" + hql.append("order by addDate desc"));
				list = getList(pageBean);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List getPlaneOrderVo(PlaneOrderVO vo) {
		// TODO Auto-generated method stub
		PlaneParentOrder parentOrder = (PlaneParentOrder) getHibernateTemplate()
				.get(PlaneParentOrder.class, vo.getId());
		List<PlaneParentOrder> parents = new ArrayList<PlaneParentOrder>();
		parents.add(parentOrder);
		vo.setParentOrders(parents);
		List<PlaneChildrenOrder> childrenOrder = new ArrayList<PlaneChildrenOrder>();
		childrenOrder = onLoad("FROM PlaneChildrenOrder where 1=1 and pid="
				+ vo.getId());
		List<PlaneOrderPassenger> orderPassengers = new ArrayList<PlaneOrderPassenger>();
		orderPassengers = onLoad("FROM PlaneOrderPassenger where 1=1 and orderId ="
				+ vo.getId());
		vo.setChildrenOrders(childrenOrder);
		vo.setOrderPassengers(orderPassengers);
		// if(parentOrder.getIsTravel()==0){
		List list = onLoad("FROM PlanItinerary WHERE orderNo='"
				+ parentOrder.getSystemSn() + "'");
		// }
		return list;
	}

	@Override
	public PlaneOrderVO getPlaneOrder(PlaneOrderVO vo) {
		// TODO Auto-generated method stub
		PlaneParentOrder parentOrder = (PlaneParentOrder) getHibernateTemplate()
				.get(PlaneParentOrder.class, vo.getId());
		List<PlaneParentOrder> parents = new ArrayList<PlaneParentOrder>();
		parents.add(parentOrder);
		vo.setParentOrders(parents);
		List<PlaneChildrenOrder> childrenOrder = new ArrayList<PlaneChildrenOrder>();
		childrenOrder = onLoad("FROM PlaneChildrenOrder where 1=1 and pid="
				+ vo.getId());
		List<PlaneOrderPassenger> orderPassengers = new ArrayList<PlaneOrderPassenger>();
		orderPassengers = onLoad("FROM PlaneOrderPassenger where 1=1 and orderId ="
				+ vo.getId());
		vo.setChildrenOrders(childrenOrder);
		vo.setOrderPassengers(orderPassengers);
		// if(parentOrder.getIsTravel()==0){
		List list = onLoad("FROM PlanItinerary WHERE orderNo='"
				+ parentOrder.getSystemSn() + "'");
		if (list != null && list.size() > 0) {
			vo.setItinerary((PlanItinerary) list.get(0));
		}
		// }
		return vo;
	}

	@Override
	public void addGaiQianInfo(PlaneGaiqianInfo gaiqianInfo) {
		getSession().save(gaiqianInfo);
	}

}
